<?php
namespace app\shop\controller;
use app\AdminRest;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Date;
use app\shop\model\Order;
use app\shop\model\RefundOrder;
use app\shop\model\User;
use app\shop\model\Wallet;
use longbingcore\wxcore\WxPay;
use think\App;
use app\shop\model\Cap as Model;
use think\facade\Db;


class AdminCap extends AdminRest
{


    protected $model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function capList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }

        if(!empty($input['status'])){

            if($input['status']==3){

                $dis[] = ['a.status','in',[3,4]];

            }else{

                $dis[] = ['a.status','=',$input['status']];
            }

        }else{

            $dis[] = ['a.status','>',-1];
        }

        $where = [] ;

        if(!empty($input['name'])){

            $where [] = ['a.store_name','like','%'.$input['name'].'%'];

            $where [] = ['a.name','like','%'.$input['name'].'%'];

            $where [] = ['a.mobile','like','%'.$input['name'].'%'];
        }

        $data = $this->model->adminDataList($dis,$where,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-30 16:50
     * @功能说明:财务中心列表
     */
    public function financeList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

        }else{

            $date_model   = new Date();

            $start_time = $date_model->where(['uniacid'=>$this->_uniacid])->min('date_str');

            $end_time   = $date_model->where(['uniacid'=>$this->_uniacid])->max('date_str');

            $end_time   +=86399;
        }

        $dis[] = ['a.status','in',[2,3]];

        $where = [] ;

        if(!empty($input['name'])){

            $where [] = ['a.store_name','like','%'.$input['name'].'%'];

            $where [] = ['a.name','like','%'.$input['name'].'%'];

            $where [] = ['a.mobile','like','%'.$input['name'].'%'];
        }

        $data = $this->model->adminDataList($dis,$where,$input['limit']);

        if(!empty($data['data'])){

            $order_model = new Order();

            $wallet_model= new Wallet();

            $refund_model= new RefundOrder();

            foreach ($data['data'] as &$v){
                //总收入
                $v['total_cash']   = $order_model->datePrice($start_time,$this->_uniacid,$v['id'],$end_time);
                //单量
                $v['total_count']  = $order_model->datePrice($start_time,$this->_uniacid,$v['id'],$end_time,0);
                //总提现
                $v['wallet_cash']  = $wallet_model->datePrice($start_time,$this->_uniacid,$v['id'],$end_time);
                //提现笔数
                $v['wallet_count'] = $wallet_model->datePrice($start_time,$this->_uniacid,$v['id'],$end_time,0);
                //退款金额
                $v['refund_price'] = $refund_model->datePrice($start_time,$this->_uniacid,$v['id'],$end_time);

                $v['refund_count'] = $refund_model->datePrice($start_time,$this->_uniacid,$v['id'],$end_time,0);

            }

        }

        return $this->success($data);

    }





    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:55
     * @功能说明:团长数量
     */
    public function capCount(){

        $dis = [

            'uniacid' => $this->_uniacid
        ];
        //所有
        $data['all_count'] = $this->model->where($dis)->where('status','>',-1)->count();
        //未授权
        $data['no_pass_count'] = $this->model->where($dis)->where('status','in',[3,4])->count();

        $dis['status'] = 1;
        //申请中
        $data['apply_count'] = $this->model->where($dis)->count();

        $dis['status'] = 2;
        //已授权
        $data['pass_count'] = $this->model->where($dis)->count();

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:58
     * @功能说明:团长详情
     */
    public function capInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-26 17:53
     * @功能说明:店铺下拉框
     */
    public function capSelect(){

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','in',[2,3]];

        $data = $this->model->where($dis)->field('id,store_name')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 15:03
     * @功能说明:修改团长
     */
    public function capUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['sh_time'] = time();

        $data = $this->model->dataUpdate($dis,$input);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-18 16:04
     * @功能说明:楼长提现列表
     */
    public function walletList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }

        $wallet_model = new Wallet();

        $data  = $wallet_model->adminList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-24 17:04
     * @功能说明:通过审核
     */
    public function walletPass(){

        $input = $this->_input;

        $wallet_model = new Wallet();

        $info = $wallet_model->dataInfo(['id'=>$input['id']]);

        if($info['status']==2){

            $this->errorMsg('已同意打款');
        }

        if($info['status']==3){

            $this->errorMsg('已拒绝打款');
        }

        $update = [

            'cash_time' => time(),

            //'text'      => !empty($input['text'])?$input['text']:'',

            'status'    => 2,

            'online'    => $input['online'],

            'pay_cash'  => $info['true_cash']
        ];

        Db::startTrans();

        $res = $wallet_model->dataUpdate(['id'=>$input['id'],'status'=>1],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('打款失败');

        }
        //线上转账
        if($input['online']==1){

            $user_model = new User();

            $openid = $user_model->where(['id'=>$info['user_id']])->value('openid');

            if(empty($openid)){

                return $this->error('用户信息错误，未获取到openid');
            }
            //微信相关模型
            $wx_pay = new WxPay($this->_uniacid);
            //微信提现
            $res    = $wx_pay->crteateMchPay($this->payConfig(),$openid,$update['pay_cash']);

            if($res['result_code']=='SUCCESS'&&$res['return_code']=='SUCCESS'){

                if(!empty($res['payment_no'])){

                    $wallet_model->dataUpdate(['id'=>$input['id']],['payment_no'=>$res['payment_no']]);
                }

            }else{

                Db::rollback();

                return $this->error(!empty($res['err_code_des'])?$res['err_code_des']:'你还未该权限');

            }

        }

        Db::commit();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-26 15:03
     * @功能说明:决绝提现
     */
    public function walletNoPass(){

        $input = $this->_input;

        $wallet_model = new Wallet();

        $info = $wallet_model->dataInfo(['id'=>$input['id']]);

        if($info['status']==2){

            $this->errorMsg('已同意打款');
        }

        if($info['status']==3){

            $this->errorMsg('已拒绝打款');
        }

        Db::startTrans();


        $update = [

            'cash_time' => time(),

            //'text'      => !empty($input['text'])?$input['text']:'',

            'status'    => 3,

        ];

        $res = $wallet_model->dataUpdate(['id'=>$input['id'],'status'=>1],$update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('打款失败');

        }

        $cap_info = $this->model->dataInfo(['id'=>$info['cap_id']]);

        $res = $this->model->dataUpdate(['id'=>$info['cap_id']],['cap_cash'=>$cap_info['cap_cash']+$info['apply_cash']]);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('打款失败');

        }

        Db::commit();


        return $this->success($res);

    }









}
